from p6_421_django.celery import app
from ronglian_sms_sdk import SmsSDK
from django.conf import settings
import json


# 创建任务函数
@app.task
def send_message(sms_code, mobile, expire=5):
    # 实例化sdk
    sdk = SmsSDK(settings.ACCID, settings.AUTHTOKEN, settings.APPID)

    # 组织数据
    tid = "1"  # 短信模板id
    mobile = "%s"%mobile
    datas = ("%s"%sms_code, "%s"%expire)

    # 发送短信，并接收响应
    res = sdk.sendMessage(tid=tid, mobile=mobile, datas=datas)

    return res

    # 解析响应
    # print("发送短信的响应:", res, type(res))
    #
    # res_dict = json.loads(res)
    # if res_dict.get("statusCode") == "000000":
    #     return True
    # else:
    #     return False

